var api = require('../../util/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        allImgMap: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        let chain = api.chain();
    	this.setData({
            page: api.page(5),
            dataList: [],
            imgMap: {},
            showAdd: chain.mode == 'EDIT' && api.hasPerm('lhjd:wx:06:03:03'),
            params : {
                examineId : chain.examineId,
                userId : chain.userId
            }
        });
        this.onReachBottom();
    },
    onPullDownRefresh(){
        this.setData({
            page: api.page(5),
            dataList: [],
            imgMap: {}
        });
        this.loadList();
    },
    onReachBottom() {
        this.loadList();
    },
    /**
     * 输入监听
     */
    searchInputAction: function (e) {
        let _this = this
        let value = e.detail.value
        if (value.length != 0) {
            _this.data.params['params[sql]'] = ` and check_record like '%${value}%'`
        }else{
            _this.data.params['params[sql]'] = ''
        }
        _this.setData({
            dataList : [],
            imgMap : {},
            page: api.page(5)
        });
        _this.loadList();
    },
    loadList(){
    var _this = this;
        var params = _this.data.params;
        var page = _this.data.page;
        api.get(`${api.ip}lhjd/fhfbb/list`, params, page).then(res => {
            page.setTotal(res.total);
            if (res.rows.length == 0) {
                this.setData({
                    hasData: false
                })
            } else {
                var picList=[];
                res.rows.forEach(function (item) {
                    var pic = item.pic;
                    if (!api.isBlank(pic)) {
                        pic.split(",").forEach(p=>{
                            if(api.isBlank(_this.data.allImgMap[p])){
                                picList.push(p);
                            }
                        });
                    }
                });
                api.preBase64ImgList(picList, function (imgMap) {
                    _this.data.allImgMap = {..._this.data.allImgMap, ...imgMap};
                    _this.setData({
                        hasData: true,
                        dataList: _this.data.dataList.concat(res.rows),
                        imgMap: _this.data.allImgMap,
                    });
                });
            }
        })
    },
    //预览图片，放大预览
    preview(e) {
        var _this = this;
        var index = e.target.dataset.index;
        var pic = e.target.dataset.pic;
        if (pic == null || pic == undefined || pic == NaN) {
            return;
        }
        var imgs = pic.split(',');
        let imgUrls = [];
        let current = "";
        for (var i = 0; i < imgs.length; i++) {
            var base64 = _this.data.imgMap[imgs[i]];
            imgUrls.push(base64);
            if (i == index) {
                current = base64;
            }
        }
        wx.previewImage({
            urls: imgUrls,
            current: current
        })
    },
    longPress(e) {
    		let mode = api.chain().mode;
        if(mode != 'EDIT' || !api.hasPerm('lhjd:wx:06:03:02')){
            return;
        }
        var id = e.currentTarget.dataset.id;
        wx.showActionSheet({
            itemList: ['编辑'],
            success: function (res) {
                if (res.tapIndex == 0) {
                    wx.navigateTo({
                        url: `/pages/lhjd/fhfbb/edit?id=${id}`
                    });
                }
            },
            fail: function (res) {
                console.log(res.errMsg)
            }
        })
    },
    goAdd() {
        let chain = api.chain();
        let examineId = chain.examineId;
        let userId = chain.userId;
        let userName = chain.userName;
        wx.navigateTo({
          url: `./edit?examineId=${examineId}&userId=${userId}&userName=${userName}`
        });
    }
})